package com.aube.interact.fighter;

import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.interact.fighter.control.DataManager;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.video.model.GoodsItem;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.utils.Utils;

/**
 * Created by hill on 16/11/3.
 */

public class CartItemView extends LinearLayout {
    private String TAG = "Aube/CartItemView";
    private Context mContext;

    private GoodsStoreLayout mParentGoodsStoreLayout;
    private LinearLayout mRoot;

    private GoodsItem mGoodsItem;
    private GoodsModel mGoodModel;

    private TextView mPurchaseText;
    private TextView mCartitemTitle, mCartitemPrice, mCartitemTimeTrack;
    private ImageView mCartitemImage;

    private IGoodsDetailRevealAnimator mAnimator;

    private boolean mBSelected = false;
    private String mTimeTrackPrefix = "出现于";
    private String mTimeTrackSuffix = " >";

    public CartItemView(Context context) {
        this(context, null);
    }

    public CartItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CartItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        mRoot = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_cartitem, this);

        mCartitemTitle = (TextView) findViewById(R.id.cartitem_title);
        mCartitemPrice = (TextView) findViewById(R.id.cartitem_price);
        mCartitemTimeTrack = (TextView) findViewById(R.id.cartitem_timetrack);
        mCartitemImage = (ImageView) findViewById(R.id.cartitem_img);
        mCartitemImage.setScaleType(ImageView.ScaleType.CENTER_CROP);

        mPurchaseText = (TextView) findViewById(R.id.cartitem_purchase);
        mPurchaseText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTxtState(!mBSelected);
                if(!mBSelected) {
                    Utils.Log(Utils.LogType.DEBUG, TAG, "mPurchaseText - cancel selection");

                    if (mAnimator != null)
                        mAnimator.removeGoods(mGoodsItem._id);
                } else {
                    Utils.Log(Utils.LogType.DEBUG, TAG, "mPurchaseText - take selection + ");

                    if (mAnimator != null)
                        mAnimator.addGoods(mGoodsItem);
                }
            }
        });

        mCartitemImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAnimator != null) {
                    // 1: fill data
                    mAnimator.prepareData(mGoodModel);

                    // 2: set up view & animate it
                    int[] location = new int[2];
                    mCartitemImage.getLocationInWindow(location);
                    int width = mCartitemImage.getWidth();
                    int height = mCartitemImage.getHeight();
                    int[] args = new int[4];
                    args[0] = location[0];
                    args[1] = location[1];
                    args[2] = width;
                    args[3] = height;
                    mAnimator.startAnimFromCartList(args, new GoodsDetailLayout.IAnimEndListener() {
                        @Override
                        public void onAnimEnd() {
                        }

                        @Override
                        public void onStartFinishAnim() {
                            // alpha in original view
                            mParentGoodsStoreLayout.startShowAnim();
                        }
                    });

                    // 3: alpha out original view
                    mParentGoodsStoreLayout.startHideAnim();
                }
            }
        });

        mCartitemTimeTrack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mParentGoodsStoreLayout.dismiss();
                if(mAnimator != null)
                    mAnimator.locateToStartPosition(mGoodModel.startSeconds);
            }
        });
    }

    public void fillData(GoodsModel data, GoodsStoreLayout layout, IGoodsDetailRevealAnimator animator) {
        mParentGoodsStoreLayout = layout;

        this.mAnimator = animator;

        mGoodModel = data;

        if(data.goodsVo != null) {
            fillInternalData(data.goodsVo);
            mCartitemTimeTrack.setText(mTimeTrackPrefix + transferSecstoMins(data.startSeconds) + mTimeTrackSuffix);
        } else {
            Utils.Log(Utils.LogType.DEBUG, TAG, "GoodsModel: goodsidList size != 1");
        }
    }

    private void fillInternalData(GoodsItem data) {
        mGoodsItem = data;

        mCartitemTitle.setText(data.tlTitle);
        CharSequence charSequence = Html.fromHtml(data.parsePrice(data.getPriceIndex()));
        mCartitemPrice.setText(charSequence);
        mCartitemPrice.setMovementMethod(LinkMovementMethod.getInstance());

        ImageLoader.getInstance(getContext()).fillImage(mCartitemImage, data.getPic(), 600, 600);

        updateTxtState(DataManager.getInstance(mContext).hasCollected(data._id));
    }

    private void updateTxtState(boolean state) {
        mBSelected = state;
        if(mBSelected) {
            mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchased);
            mPurchaseText.setText(mGoodModel.getAfterDesc());
        } else {
            mPurchaseText.setBackgroundResource(R.drawable.bg_cartitem_purchase);
            mPurchaseText.setText(mGoodModel.getBeforeDesc());
        }
    }

    public void update() {
        if(mGoodsItem != null) {
            updateTxtState(DataManager.getInstance(getContext()).hasCollected(mGoodsItem._id));
        }
    }

    private String transferSecstoMins(int value) {
        int min = value / 60;
        int sec = value % 60;
        String str = String.format("%02d:%02d", min, sec);
        return str;
    }
}
